package com.sjksy.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sjksy.demo.domain.*;
import com.sjksy.demo.mapper.GuideMapper;
import com.sjksy.demo.mapper.TravelContractMapper;
import com.sjksy.demo.msmapper.travel_contract.TravelMessageMsMapper;
import com.sjksy.demo.service.TravelMessageService;
import com.sjksy.demo.mapper.TravelMessageMapper;
import com.sjksy.demo.service.TravelRouteService;
import com.sjksy.demo.vo.travel_message.TravelInfoInsertVo;
import com.sjksy.demo.vo.travel_message.TravelMessageVo;
import com.sjksy.demo.vo.travel_route.TravelRouteResponseVo;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author guyueyuan
* @description 针对表【travel_message】的数据库操作Service实现
* @createDate 2024-12-11 15:37:03
*/
@Service
public class TravelMessageServiceImpl extends ServiceImpl<TravelMessageMapper, TravelMessage>
    implements TravelMessageService{

    @Resource
    private TravelRouteService travelRouteService;
    @Resource
    private TravelMessageMapper travelMessageMapper;
    @Resource
    private TravelContractMapper travelContractMapper;
    @Resource
    private TravelMessageMsMapper travelMessageMsMapper;
    @Resource
    private GuideMapper guideMapper;

    /**
     * 获取供顾客选择的所有线路以及景点信息
     * @return
     */
    public List<TravelMessageVo> getInformation() {

        List<TravelMessageVo> list=new ArrayList<>();
        // 这里加了个过度
        QueryWrapper<TravelMessage> wrapper=new QueryWrapper<>();
        wrapper.gt("travel_start_time",new Date());
        List<TravelMessage> travelMessages=travelMessageMapper.selectList(wrapper);

        return getTravelMessageVos(list, travelMessages);
    }

    @Override
    public boolean insertTravelInfo(TravelInfoInsertVo travelInfoInsertVo) {
        return travelMessageMapper.insert(travelMessageMsMapper.VoInsertInfoToTravelMessage(travelInfoInsertVo))>=1;
    }

    @Override
    public boolean updateTravelInfo(TravelInfoInsertVo travelInfoInsertVo) {
        return travelMessageMapper.updateById(travelMessageMsMapper.VoInsertInfoToTravelMessage(travelInfoInsertVo))>=1;
    }

    @Override
    public List<TravelMessageVo> getAllTravelInfo() {
        List<TravelMessageVo> list=new ArrayList<>();

        List<TravelMessage> travelMessages=travelMessageMapper.selectList(null);

        return getTravelMessageVos(list, travelMessages);
    }

    private List<TravelMessageVo> getTravelMessageVos(List<TravelMessageVo> list, List<TravelMessage> travelMessages) {
        for (TravelMessage travelMessage:travelMessages){
            TravelContract travelContract=travelContractMapper.selectById(travelMessage.getTravelContractId());

            TravelRouteResponseVo travelRouteResponseVo=travelRouteService.getTravelRouteById(travelContract.getRouteId());

            TravelMessageVo travelMessageVo=new TravelMessageVo();
            travelMessageVo.setTravelId(travelMessage.getTravelId());
            travelMessageVo.setMoney(BigDecimal.valueOf(travelContract.getMoney()));

            Guide guide = guideMapper.selectById(travelContract.getGuideId());
            travelMessageVo.setGuideLevel(guide.getGuideLevel());

            String ls=travelRouteResponseVo.getRouteName();

            List<String> spots= travelRouteResponseVo.getSpots().stream()
                    .map(Spots::getSpotName)
                    .collect(Collectors.toList());

            String l= StringUtils.join(spots,",");
            travelMessageVo.setMessage(l);
            travelMessageVo.setRouteName(ls);
            travelMessageVo.setTravelStartTime(travelMessage.getTravelStartTime());
            travelMessageVo.setTravelEndTime(travelMessage.getTravelEndTime());
            list.add(travelMessageVo);
        }
        return list;
    }

}




